/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package privileges.bean;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import login.bean.LoginBean;
import login.bean.PrincipalPageBean;
import privileges.business.UserBusiness;

/**
 *
 * @author Javier
 */
@ManagedBean
@RequestScoped
public class ChangePasswordBean {
    
    @ManagedProperty(value="#{advancedPrivilegesBean.nameUser}")
    private String user;
    @ManagedProperty(value="#{advancedPrivilegesBean.host}")
    private String host;
    private AdvancedPrivilegesBean ap;
    private String password;
    private String reTypePassword;
    private String actualUser;
    private String actualHost;


    public ChangePasswordBean() {
        ap = (AdvancedPrivilegesBean) FacesContext.getCurrentInstance()
                .getExternalContext().getSessionMap().get("advancedPrivilegesBean");
    }
    
    @PostConstruct
    private void getActualUser(){
        actualUser = user;
        actualHost = host;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getReTypePassword() {
        return reTypePassword;
    }

    public void setReTypePassword(String reTypePassword) {
        this.reTypePassword = reTypePassword;
    } 
    
    public String changePass(){
        FacesContext context = FacesContext.getCurrentInstance();
        String mess;
        if(password.equals(reTypePassword)){
            mess = new UserBusiness().changePassword(user,actualUser,host,actualHost,password);
            context.addMessage(null, new FacesMessage("Mensaje", mess));
            PrincipalPageBean.centerPage = "advancedPrivileges";
             //ap.setNameUser(user);
             //ap.setHost(host);
              ap.reloadTables();
              ap.setIndex(0);
              ap.setDisabledParent("false");
              ap.setDisabled("true");
              ap.setSelectedSchema("");       
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.getExternalContext().getSessionMap().containsKey("columnPrivilegesBean")) {
            System.out.println("entro a borrar");
            fc.getExternalContext().getSessionMap().remove("columnPrivilegesBean");
        }
        FacesContext fc2 = FacesContext.getCurrentInstance();
        if (fc2.getExternalContext().getSessionMap().containsKey("advancedPrivilegesBean")) {
            System.out.println("entro a borrar adavanced");
            fc2.getExternalContext().getSessionMap().remove("advancedPrivilegesBean");
        }
        return "principalPage.xhtml?faces-redirect=true";
        }
        else
            context.addMessage(null, new FacesMessage("Error", "Las claves no coinciden"));
        return "";
    }
}
